package com.naiterui.ehp.bs.search.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bp.utils.thread.ThreadPoolUtil;
import com.naiterui.ehp.bs.search.service.IDrDiagnosisService;
import com.naiterui.ehp.bs.search.utils.PropertyValueConstants;
import com.naiterui.ehp.bs.search.vo.DiagnosisVO;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guoyongxiang
 */
@Api(tags = SwaggerTag.DOCTOR + SwaggerTag.DIAGNOSIS)
@RequestMapping("search/diagnosis")
@RestController
public class SearchDiagnosisController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SearchDiagnosisController.class);

    @Autowired
    private IDrDiagnosisService diagnosisService;

    /**
     * 标准库索引创建(批量导入时候调用)
     *
     * @return
     */
    @RequestMapping("findStandardIndexData")
    public ResponseVO<Long> findStandardIndexData() throws BusinessException {
        long start = System.currentTimeMillis();
        LOGGER.info("findStandardIndexData start.");
        long count = this.diagnosisService.findStardandDiagnosisIndex();

        LOGGER.info("findStandardIndexData end. cost:" + (System.currentTimeMillis() - start));
        return PublicService.returnResponseVO(count);
    }

    @RequestMapping("findGb95IndexData")
    public ResponseVO<Long> findGb95IndexData() throws BusinessException {
        long start = System.currentTimeMillis();
        LOGGER.info("findGb95IndexData start.");
        long count = this.diagnosisService.findDiagnosisGb95Index();

        LOGGER.info("findGb95IndexData end. cost:" + (System.currentTimeMillis() - start));
        return PublicService.returnResponseVO(count);
    }

    /**
     * 索引搜索
     *
     * @param doctorId
     * @param key
     * @param skuIds
     * @param paramVo
     * @param type     诊断库类别(0: ICD10-默认, 2: GB95)
     * @return
     */
    @RequestMapping("findDiagnosisList")
    public ResponseVO<PageVO<DiagnosisVO>> findDiagnosisList(Long doctorId, String key, Integer type, String skuIds, PageParamVO paramVo) throws BusinessException {
        long start = System.currentTimeMillis();
        if (type == null) {
            type = PropertyValueConstants.DIAGNOSIS_TYPE_ICD10;
        }
        LOGGER.info("findDiagnosisList params is doctorId {} key {} skuIds {} paramVo {}", doctorId, key, skuIds, paramVo);

        // 当关键为空时，返回空信息，不进行搜索
        if (StringUtils.isEmpty(key)) {
            return PublicService.returnResponseVO(new PageVO<>());
        }
        List<Long> skuIdList = new ArrayList<>();
        if (!StringUtils.isBlank(skuIds)) {
            skuIdList = StringUtil.convertLongStr2List(skuIds, ",");
        }
        PageVO<DiagnosisVO> list = null;
        if (PropertyValueConstants.DIAGNOSIS_TYPE_GB95.equals(type)) {
            list = this.diagnosisService.searchDiagnosisGb95List(key, paramVo);
        } else {
            list = this.diagnosisService.searchDiagnosisList(doctorId, key, skuIdList, paramVo);
        }
        LOGGER.info("findDiagnosisList end. cost:" + (System.currentTimeMillis() - start));
        return PublicService.returnResponseVO(list);
    }

    @PostConstruct
    public void initIndexStandard() {
        ThreadPoolUtil.execute(() -> {
            try {
                LOGGER.info("索引初始化开始-ICD10...");
                long count = this.diagnosisService.findStardandDiagnosisIndex();
                LOGGER.info("索引初始化完成-ICD10 count:{}", count);
            } catch (Exception e) {
                LOGGER.warn("索引初始化失败-ICD10", e);
            }
        });
    }

    //@PostConstruct
    public void initIndexGb95() {
        ThreadPoolUtil.execute(() -> {
            try {
                LOGGER.info("索引初始化开始-GB95...");
                long count = this.diagnosisService.findDiagnosisGb95Index();
                LOGGER.info("索引初始化完成-GB95 count:{}", count);
            } catch (Exception e) {
                LOGGER.warn("索引初始化失败-GB95", e);
            }
        });
    }
}
